package cn.com.generaldata.os.service.indicator_evaluation.development_index;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.Validate;
import org.nutz.castor.Castors;
import org.nutz.dao.entity.Record;
import org.nutz.dao.sql.Criteria;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 一级海洋经济发展指数指标计算类
 * */
public class PrimaryDevelopmentIndexCalculateService extends IndicatorEvaluationCalculateService {

	private List<Record> developmentIndexList;

	
	@Override
	protected Double calculate(IndicatorEvaluation indicator,
			IndicatorAccountVO indicatorAccountVO) {
		// TODO Auto-generated method stub
		if (developmentIndexList==null){
			developmentIndexList = initDevelopmentIndexInfo();
		}
		
		//如果是基期，直接返回基期对应的基数值
		if (indicatorAccountVO.getYear().intValue()==ProvinceIndicatorConstant.DEVELOPMENT_INDEX_BASE_PERIOD){
			return ProvinceIndicatorConstant.DEVELOPMENT_INDEX_BASE_RADICES;
		}
		
		List<Record> twoLevelList = querySubDevelopmentIndexList(indicator);
		
		Double weight = 0.0;
		Double vl = 0.0;
		for (Record rc : twoLevelList){
			Double twoLevelWeight = Castors.me().castTo(rc.get("di_weight"), Double.class);
			Double twoLevelValue = getCityDevelopmentIndexValue(rc.getString("DI_NAME"), indicatorAccountVO);
			weight = weight + twoLevelWeight;
			
			vl = vl + twoLevelValue * twoLevelWeight;
		}
		
		return vl / weight;
	}

	private List<Record> initDevelopmentIndexInfo(){		
		List<Record> list = dao.query("t_indicator_di_info", null);
		return list;
	}
	
	
	private List<Record> querySubDevelopmentIndexList(IndicatorEvaluation indicator){
		
		List<Record> subList = new ArrayList<Record>();
		//查询发展指数Id
		int fid=0;
		for (Record rc : developmentIndexList){
			if (indicator.getName().equals(rc.getString("DI_NAME"))){
				fid = rc.getInt("ID");
			}
		}
		
		//根据父Id查询下级所有发展指数信息（二级发展指数）
		for (Record rc : developmentIndexList){
			if (rc.getInt("di_parent")==fid){
				subList.add(rc);
			}
		}
		
//		//计算所有二级发展指数权重
//		for (Record rc : subList){
//			
//			for (Record rc2 : developmentIndexList){
//				
//				Double weight =  Castors.me().castTo(rc.get("di_weight"), Double.class);
//				
//				if (weight==null){weight=0.0;}
//				
//				if (rc.getInt("ID")==rc2.getInt("di_parent")){
//					weight = weight + Castors.me().castTo(rc2.get("di_weight"), Double.class);
//				}
//				
//				rc.set("di_weight", weight);
//			}
//			
//		}
		
		return subList;
	}
	
	/**
	 * 获取评估指标对应的值
	 * 
	 * @param indicatorAccountVO
	 * @return
	 */
	private Double getCityDevelopmentIndexValue(String diName, IndicatorAccountVO indicatorAccountVO) {
		Integer indicatorId = getIndicatorEvaluationId(diName, indicatorAccountVO.getAreaId());
		Criteria cri = getIndicatorDataCri(indicatorAccountVO, indicatorAccountVO.getYear(), indicatorId);
		Double result = getEvaluationIndicatorValue(cri);
		Validate.isTrue(result != null);
		return result;
	}
}
